//classes
function Poi() {
	this.id = null;
	this.lon = null;
	this.lat = null;
	this.marker = null;
	this.version = null;
	this.data = {};
	this.getType = function(){
		if (this.data.amenity != undefined) {
			return settings.pois[this.data.amenity];
		} else if (this.data.railway != undefined) {
			return settings.pois[this.data.railway];
		} else if (this.data.tourism != undefined) {
			return settings.pois[this.data.tourism];
		} else if (this.data.shop != undefined) {
			return settings.pois[this.data.shop];
		} else if (this.data.highway != undefined) {
			return settings.pois[this.data.highway];
		}
		return null;
	};
	this.getOmittedTags = function() {
		return new Array('name', 'amenity', 'addr:street', 'addr:housenumber',
				'addr:postcode', 'phone', 'website', 'description', 'shop', 'tourism', 'railway', 'highway');
	}
	this.getXML = function(changeSetId) {
		var xml = '<osm><node changeset="' + changeSetId + '" lon="' + this.lon + '" lat="' + this.lat + '">';
		for (key in this.poi.data) {
			if (this.data[key] != undefined && poi.data[key].length > 0) {
				xml = xml + '<tag k="' + key + '" v="' + this.data[key] + '" />';
			}
		}
		return (xml + "</node></osm>");
	};
	this.getIcon = function() {
		var type = this.getType();
		return settings.poiIconsFolder + type['icon'];
	}
};

function PoiSet() {
	this.pois = {};
	this.put = function(poi) {
		this.pois[poi.id] = poi;
	};
	this.has = function(poiId) {
		if (this.pois[poiId] === undefined || this.pois[poiId] == null) {
			return false;
		}
		return true;
	}
}
		
function WaitingWidget(container) {
	this.container = container;
	this.floatWidget = new FloatWidget(container);
	this.message = 'Loading';
	this.intervalId = 0;
	this.changeColor = function(){
		if ($('#loading').hasClass('white')) {
			$('#loading').removeClass('white');
			$('#loading').addClass('black');
		} else {
			$('#loading').removeClass('black');
			$('#loading').addClass('white');
		}
	},
	this.wait = function(){
		this.run = true;
		this.floatWidget.display('<span class="loading">' + this.message + '</span>');
		this.intervalId = setInterval(this.changeColor, settings.loadingFrequency);
	},
	this.stop = function(){
		clearInterval(this.intervalId);
		}
}

function FloatWidget(container, options) {
	this.container = container;
	if (options === undefined) {
		this.options = {
			align: 'top',
			spacing: 30
		}
	} else {
		this.options = options;
	}
	//handles displaying hint        
    $(this.container).makeFloaty(this.options); 
    $(this.container).click(function() {
    	$(this).hideFloaty();
    });
    //to hide initialy hint
    $(this.container).css('display', 'none');
    $(this.container).hideFloaty();
    this.display = function(text){
		this.hide();
		$(this.container).html(text); 
		$(this.container).css('display', 'inherit');
    	$(this.container).toggleFloaty();
        return false; 
  	}; 
  	this.hide = function() {
  		$(this.container).hideFloaty();
    }
}

function DialogWindow(container, options) {
	this.floatWidget = new FloatWidget(container, options);
	this.display = this.floatWidget.display;
	this.hide = this.floatWidget.hide;
	this.container = container;
	$(this.container + " li.cancelBtn").click(function() {
		this.hide();
	});
	$(this.container + " li.editBtn").click(function() {
		this.clickHandle();
	});
	this.setData = function(data) {
		$(this.container + ' span#poiDataId').text(data.id);
		$(this.container + ' span#poiDataName').text(data.name);
		$(this.container + ' span#poiDataType').text(data.type);
	};
	this.setClickHandle = function(clickHandle) {
		$(this.container + " li.editBtn").unbind('click');
		$(this.container + " li.editBtn").click(clickHandle);
	}
}

function EditForm(container){
	this.container = container;
	this.poi = null;
	this.clear = function(){
		$(this.container + " input[name=name]").val('');
		$(this.container + " #typeValue").text('');
		$(this.container + " #typeName").text('');
		$(this.container + " #typeTag").text('');
		$(this.container + " input[name=street]").val('');
		$(this.container + " input[name=houseNumber]").val('');
		$(this.container + " input[name=postCode]").val('');
		$(this.container + " input[name=phone]").val('');
		$(this.container + " input[name=website]").val('');
		$(this.container + " textarea[name=description]").val('');
		//removing old additional tags from form
		$(this.container + " ul.moreTags li.showableField").each(function(){
			$(this).remove();
		});
		$(this.container + " ul.moreTags").append('<li class="formRow arrow plus showableField hidden addNewTag">Add tag</li>');
		$(this.container + " li.addNewTag").click(function(){
			$(this).before('<li class="formRow showableField additionalTag">' +
						    '<input type="text" class="inputForm leftInput key" placeholder="' + message.inputs.tagName + '"/>' +
							'<input type="text" class="inputForm rightInput value" placeholder="' + message.inputs.tagValue + '"/></li>');
		});
	};
	this.getData = function() {
		data = {};
		data['name'] = $(this.container + " input[name=name]").val();
		data[$(this.container + " #typeTag").text()] = $(this.container + " #typeValue").text();
		data['addr:street'] = $(this.container + " input[name=street]").val();
		data['addr:housenumber'] = $(this.container + " input[name=houseNumber]").val();
		data['addr:postcode'] = $(this.container + " input[name=postCode]").val();
		data['phone'] = $(this.container + " input[name=phone]").val();
		data['website'] = $(this.container + " input[name=website]").val();
		data['description'] = $(this.container + " textarea[name=description]").val();
		$(this.container + " li.additionalTag").each(function(){
			key = $(this).children("input.key").val();
			value = $(this).children("input.value").val();
			if (key != '' && value != '') {
				data[key] = value;
			}
		});
		return data;
	};
	this.fill = function(poi) {
		this.clear();
		$(this.container + " input[name=name]").val(poi.data.name);
		$(this.container + " #typeValue").text(poi.getType().tagValue);
		$(this.container + " #typeTag").text(poi.getType().tagKey);
		$(this.container + " #typeName").text(poi.getType().name);
		$(this.container + " input[name=street]").val(poi.data['addr:street']);
		$(this.container + " input[name=houseNumber]").val(poi.data['addr:housenumber']);
		$(this.container + " input[name=postCode]").val(poi.data['addr:postcode']);
		$(this.container + " input[name=phone]").val(poi.data['phone']);
		$(this.container + " input[name=website]").val(poi.data['website']);
		$(this.container + " textarea[name=description]").val(poi.data['description']);
		//function checks if element is in array
		var inArray = function(ele, array) {
			for (k in array) {
				if (array[k] == ele) {
					return true;
				}
			}
			return false;
		}

		//fields which shouldn't be put in "more tags" part of the form
		var omittedTags = poi.getOmittedTags();
		var key;
		for (key in poi.data) {
			if (!inArray(key, omittedTags)) {
				$(this.container + " li.addNewTag").before('<li class="formRow showableField hidden additionalTag">' +
				'<input type="text" class="inputForm leftInput key" value="' + key + '"/>' +
				'<input type="text" class="inputForm rightInput value" value="' + poi.data[key] + '"/></li>');
			}
		}
	}
};



function Map(touchMap) {
	this.id = 'viewer';
	this.calculateProjection = new OpenLayers.Projection("EPSG:4326");
	this.displayProjection = new OpenLayers.Projection("EPSG:900913");
	this.touchMap = touchMap;
	this.markers = {};
	this.previousLon = null;
	this.previousLat = null;
	this.previousZoom = null;
	this.layer = null;
	this.init = function() {
		
	};
	this.setClickListenerMarker = function(marker) {
		
	}
	this.getZoom = function() {
		return this.touchMap.getZoom();
	};
	this.centerTo = function(lon, lat, zoom) {
		if (!zoom) {
			zoom = this.touchMap.getZoom();
		}
		this.touchMap.setCenter(new OpenLayers.LonLat(lon, lat).transform(this.calculateProjection, this.displayProjection), zoom);
	}
	this.getWidth = function() {
	
	};
	this.getHeight = function() {
		
	};
	this.hasMoved = function() {
		var center = this.touchMap.getCenter().transform(this.displayProjection, this.calculateProjection);
		if (center.lon != this.previousLon || center.lat != this.previousLat || this.getZoom() != this.previousZoom) {
			this.previousLon = center.lon;
			this.previousLat = center.lat;
			this.previousZoom = this.getZoom();
			return true;
		}
		return false;
	};
	this.getBoundary = function() {
		return this.touchMap.getExtent().transform(this.displayProjection, this.calculateProjection);
	};
	this.setMarkersLayer = function(layer) {
		this.markersLayer = layer;
		this.touchMap.addLayer(layer);
	}

}



function Marker(id, icon) {
	this.id = id;
	this.icon = icon;
	this.iconWidth = 32;
	this.iconHeight = 32;
	this.x = null;
	this.y = null;
	this.lonLat = null
	this.map = null;
	this.marker = null;
	this.html = '<img id="' + this.id + '" src="' + this.icon + '" style="position: absolute; z-index:1500"/>';
	this.init = function(map){
		map.markers[this.id] = this;
		this.map = map;
	};
	this.getLon = function() {
		return this.lonLat.lon;
	};
	this.getLat = function() {
		return this.lonLat.lat;
	};
	this.putInTheCenter = function() {
		var mapWidth = this.map.getWidth();
		var mapHeight = this.map.getHeight();
		var topMargin = $(".toolbar").innerHeight();
		this.putOnMapByPixels(Math.round(mapWidth / 2), Math.round(mapHeight / 2) + topMargin);
		
	};
	this.putOnMap = function(lon, lat) {
		if (this.marker != null) {
			this.map.markersLayer.removeMarker(this.marker);
		}
		this.lonLat = new OpenLayers.LonLat(lon, lat);
		var iconObject = new OpenLayers.Icon(this.icon, new OpenLayers.Size(this.iconWidth, this.iconHeight), new OpenLayers.Pixel(-(this.iconWidth/2), -this.iconHeight));
		this.marker = new OpenLayers.Marker(this.lonLat.clone().transform(this.map.calculateProjection, this.map.touchMap.getProjectionObject()), iconObject);
		this.map.markersLayer.addMarker(this.marker);
	};
	this.show = function() {
		$("#" + this.id).show();
	}
	this.hide = function() {
		if (this.map) {
			this.map.markersLayer.removeMarker(this.marker);
			this.marker = null;
		}
	}
	this.tempX = null;
	this.tempY = null;
	this.moveActive = false;
	this.viewerMoved = function(e){
		this.hide();
	};
	this.viewerZoomed = function(e){
		this.refresh();
	};
	this.refresh = function(){
		//this.putOnMap(this.lon, this.lat);
	}
	this.display = function() {
		var mapHeight = this.map.getHeight();
		var mapWidth = this.map.getWidth();
		if (this.x <= - this.iconWidth || this.x >= mapWidth) {
			this.hide();
		}
		if (this.y <= - this.iconHeight || this.y >= mapHeight - this.iconHeight) {
			this.hide();
		} else {
			this.show();
		}
	};
	this.setClickEvent = function(fun) {
		$("#" + this.id).unbind('click');
		$("#" + this.id).bind('click', fun);
	};
	this.setClickEvent = function(func) {
		this.marker.events.register('click', this, func);
	}
}